package com.heroCW.bodhiAttic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heroCW.bodhiAttic.dto.DishDto;
import com.heroCW.bodhiAttic.dto.SetmealDto;
import com.heroCW.bodhiAttic.entity.Setmeal;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.CategoryService;
import com.heroCW.bodhiAttic.service.DishService;
import com.heroCW.bodhiAttic.service.SetmealDishService;
import com.heroCW.bodhiAttic.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto: {}", setmealDto);

        Boolean flag = setmealService.saveWithDish(setmealDto);
        if (flag) {
            return Result.success(ResultCode.SAVE_OK, "新增套餐成功");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "新增套餐失败");
        }
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<SetmealDto>> page(int page, int pageSize, String name) {
        // 创建分页构造器
        IPage<SetmealDto> dtoPage = setmealService.getPage(page, pageSize, name);

        if (dtoPage != null) {
            return Result.success(ResultCode.GET_OK, dtoPage);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到套餐数据");
        }
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);

        Boolean flag = setmealService.removeWithDish(ids);
        if (flag) {
            return Result.success(ResultCode.DELETE_OK, "套餐数据删除成功");
        } else {
            return Result.error(ResultCode.DELETE_ERR, "套餐数据删除失败");
        }
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);

        Boolean flag = setmealService.updateWithDish(setmealDto);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "修改套餐成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "修改套餐失败");
        }

    }

    /**
     * 修改或批量修改套餐状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, @RequestParam("ids") List<Long> ids) {

        Boolean flag = setmealService.updateStatus(status, ids);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "套餐状态修改成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "套餐状态修改失败");
        }
    }

    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Setmeal setmeal) {

        List<Setmeal> list = setmealService.getList(setmeal);

        if (list != null) {
            return Result.success(ResultCode.GET_OK, list);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到套餐数据");
        }
    }

    /**
     * 根据id查询套餐信息及其菜品信息（回显信息）
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable Long id) {
        log.info("套餐id：{}", id);

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        if (setmealDto != null) {
            return Result.success(ResultCode.GET_OK, setmealDto);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到套餐数据");
        }

    }

    @GetMapping("/dish/{id}")
    public Result<List<DishDto>> getSetmealDish(@PathVariable Long id) {
        List<DishDto> dtoList = setmealService.getSetmealDish(id);

        if (dtoList != null) {
            return Result.success(ResultCode.GET_OK, dtoList);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到套餐数据");
        }
    }
}
